package com.jade.editor.anisotropy.node;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wb.swt.SWTResourceManager;

public class Node {

	public int x = 0;
	public int y = 0;
	private Composite _point = null;
	private Composite _parent = null;
	private RGB _color = new RGB(0,0,0);
	private RGB _colorSelect = new RGB(255, 0, 0);
	private boolean _pressed = false;
	private int _mx = 0;
	private int _my = 0;
	private Collector _col;
	private Node thiz;
	private int _xb = 0;
	private int _yb = 0;
	private boolean _moveAllowed = false;
	
	private Cortege _c1 = null;
	private Cortege _c2 = null;
	
	public void setCortegeFirst(Cortege c){
		_c1 = c;
	}
	
	public void setCortegeSecond(Cortege c){
		_c2 = c;
	}
	
	public Cortege getFirstCortege(){
		return _c1;
	}
	
	public Cortege getSecondCortege(){
		return _c2;
	}
	
	public boolean hasFirstCortege(){
		if (_c1 != null)
			return true;
		return false;
	}
	
	public boolean hasSecondCortege(){
		if (_c2 != null)
			return true;
		return false;
	}
	
	public void freeCoteges(){
		_c1 = null;
		_c2 = null;
	}

	
	public void addCortege(Cortege c){
		if (_c1 == null)
			_c1 = c;
		else if (_c2 == null)
			_c2 = c;
	}
	
	public boolean hasBothCorteges(){
		if(hasFirstCortege() && hasSecondCortege())
			return true;
		return false;
	}
	
	public void setAllowMove(boolean b){
		_moveAllowed = b;
	}
	
	public void setColor(RGB color){
		_color = color;
		_point.setBackground(SWTResourceManager.getColor(_color));
		_point.setForeground(SWTResourceManager.getColor(_color));
	}
	
	public void setSelectionColor(RGB color){
		_colorSelect = color;
	}
	
	public void punish(){
		if (_point != null)
			_point.dispose();
		_point = null;
	}
	
	public Node(Composite parent, Collector collector, int x, int y, RGB color, RGB colorSelect){
		_point = new Composite(parent, SWT.NONE);
		_col = collector;
		_parent = parent;
		this.x = x;
		this.y = y;
		
		_color = color;
		_colorSelect = colorSelect;
		
		_point.setBounds(x-2, y-2, 10, 10);
		_point.setBackground(SWTResourceManager.getColor(_color));
		Region r = new Region();
		r.add(circle(3,3,3));
		_point.setRegion(r);
		addListeners();
		thiz = this;
		
	}
	
	public Node(int x, int y){																				//+
		this.x = x;																							//+
		this.y = y;																							//+
	}
	
	public void init(Composite parent, Collector collector, RGB color, RGB colorSelect){				//+
		_point = new Composite(parent, SWT.NONE);															//+
		_col = collector;																					//+
		_parent = parent;																					//+
		
		_color = color;																						//+
		_colorSelect = colorSelect;																			//+
		
		_point.setBounds(x-2, y-2, 10, 10);																	//+
		_point.setBackground(SWTResourceManager.getColor(_color));											//+
		Region r = new Region();																			//+
		r.add(circle(3,3,3));																				//+
		_point.setRegion(r);																				//+
		addListeners();																						//+
		thiz = this;																						//+
	}
	
	
	
	public void stepBack(){
		x = _xb + 2;
		y = _yb + 2;
		if (_c1 != null)
			_c1.reCulc();
		if (_c2 != null)
			_c2.reCulc();
		_point.setLocation(_xb, _yb);
		_parent.redraw();
	}

	
	private void addListeners(){
		_point.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				_point.setBackground(SWTResourceManager.getColor(_colorSelect));
				_pressed = _pressed ? false : true;
				_mx = e.x;
				_my = e.y;
				_xb = _point.getLocation().x;
				_yb = _point.getLocation().y;
				_col.preSelect(thiz);
				_point.setToolTipText(_xb + " ; " + _yb);
				
			}
			
			@Override
			public void mouseUp(MouseEvent e){
				_point.setBackground(SWTResourceManager.getColor(_color));
				_pressed = _pressed ? false : true;
				x = _point.getLocation().x + 2;
				y = _point.getLocation().y + 2;
				_col.select(thiz);
				if (_c1 != null)
					_c1.reCulc();
				if (_c2 != null)
					_c2.reCulc();
				_parent.redraw();
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if(_moveAllowed){
					NodeSettings ns = new NodeSettings(_parent.getShell(), x, y, _parent.getSize());
					ns.open();
					if (ns.getReturnCode() == Window.OK){
						_point.setLocation(ns.getX() - 2, ns.getY() - 2);
						x = ns.getX();
						y = ns.getY();
						if (_c1 != null)
							_c1.reCulc();
						if (_c2 != null)
							_c2.reCulc();
						_point.setBackground(SWTResourceManager.getColor(_color));
						_parent.redraw();
					}
				}
				_pressed = false;
				
			}
		});
		
		_point.addMouseMoveListener(new MouseMoveListener() {
			public void mouseMove(MouseEvent e) {
				if (_pressed && _moveAllowed){
					int xx = _point.getLocation().x + (e.x - _mx);
					int yy = _point.getLocation().y + (e.y - _my);
					_point.setLocation(xx, yy);
					x = xx + 2;
					y = yy + 2;
					_point.setToolTipText(x + " ; " + y);
					_parent.redraw();
				}
				
			}
		});
		
	}
	
	public boolean equals(Object o){
		
		if (o instanceof Node){
			Node n = (Node) o;
			
			if(this.x == n.x && this.y == n.y)
				return true;
		}
		return false;
	}
	
	public String toString(){
		return "[" + x + " ; " + y + "]";
	}
	
	private int[] circle(int r, int offsetX, int offsetY) {
		int[] polygon = new int[8 * r + 4];
		for (int i = 0; i < 2 * r + 1; i++) {
			int x = i - r;
			int y = (int) Math.sqrt(r * r - x * x);
			polygon[2 * i] = offsetX + x;
			polygon[2 * i + 1] = offsetY + y;
			polygon[8 * r - 2 * i - 2] = offsetX + x;
			polygon[8 * r - 2 * i - 1] = offsetY - y;
			}
		return polygon;
	}
	
}